package com.ydd.service.Impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.ArticlePageDTO;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddCate;
import com.ydd.exception.ArticleNotFoundException;
import com.ydd.mapper.YddArticleMapper;
import com.ydd.result.ArticlePage;
import com.ydd.service.YddArticleService;
import com.ydd.vo.YddArticleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-05-02 14:51
 */
@Service
public class YddArticleServiceImpl implements YddArticleService {
    @Autowired
    private YddArticleMapper yddArticleMapper;

    /**
     * 文章列表-分页
     *
     * @param articlePageDTO
     * @return
     */
    @Override
    public ArticlePage list(ArticlePageDTO articlePageDTO) {

        PageHelper.startPage(articlePageDTO.getPage(), articlePageDTO.getLimit());
        List<YddArticleVO> list = yddArticleMapper.list(articlePageDTO);
        Page<YddArticleVO> page = (Page<YddArticleVO>) list;

        for (YddArticleVO yddArticleVO : list) {
            YddCate yddCate = yddArticleMapper.getByCateId(yddArticleVO.getCateid());
            yddArticleVO.setCatename(yddCate.getCatename());
        }

        return ArticlePage.builder()
                .count(page.getTotal())
                .totalPages(page.getPages())
                .pageSize(page.getPageSize())
                .currentPage(page.getPageNum())
                .data(page.getResult())
                .build();
    }

    /**
     * 文章删除
     *
     * @param id
     */
    @Override
    public void del(String id) {
        YddArticle yddArticle = yddArticleMapper.getByArticleId(id);
        if (yddArticle == null) {
            throw new ArticleNotFoundException("文章不存在");
        }
        yddArticleMapper.deleteById(id);
    }

    /**
     * 文章详情
     *
     * @param id
     * @return
     */
    @Override
    public YddArticle rowInfo(String id) {
        //YddArticle yddArticle = yddArticleMapper.getByArticleId(id);
        YddArticle yddArticle = yddArticleMapper.selectById(id);
        return yddArticle;
    }

    /**
     * 文章修改
     *
     * @param yddArticle
     */
    @Override
    public void update(YddArticle yddArticle) {
        yddArticleMapper.updateById(yddArticle);
    }
}
